<template>
    <div>
        <el-form :model="formData" :rules="rules" inline="true" ref="formData"
                 class="demo-form-inline" style="margin-left: 100px">

            <el-row style="">
                <el-col>
                    <el-form-item label="上课周期">
                        <el-select size="mini"  @change="daychange" style="width: 200px;" v-model="formData.day" placeholder="请选择周期">
                            <el-option
                                v-for="item in daysList"
                                :key="item.id"
                                :label="item.name"
                                :value="item.id">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row style="">
                <el-col>
                    <el-form-item label="上课时间">
                        <el-time-select style="width: 200px;"
                            size="mini" @blur="timechange"
                            v-model="formData.time"
                            :picker-options="{
    start: '08:30',
    step: '00:15',
    end: '18:30'
  }"
                            placeholder="选择时间">
                        </el-time-select>
                    </el-form-item>
                </el-col>
            </el-row>

            <el-row style="">
                <el-col>
                    <el-form-item label="所属总校">
                        <el-select size="mini" style="width: 200px;" v-model="formData.belongschoolid" placeholder="请选择学校">
                            <el-option
                                v-for="item in masterschoolList"
                                :key="item.id"
                                :label="item.name"
                                :value="item.id">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row style="margin-top: 20px;">
                <el-col>
                    <el-form-item label="时段名称">
                        <el-input size="mini" style="width: 200px;" v-model="formData.name" placeholder="必填"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col>
                    <el-button type="primary" style="margin-left: 200px" @click="submitForm('add')">提交<i
                        class="el-icon-upload el-icon--right"></i></el-button>
                </el-col>
            </el-row>

        </el-form>
    </div>
</template>
<script>
    import {mapActions, mapState, mapGetters} from 'vuex'
    import {addPeriod} from '@/api/getData'

    export default {
        data() {
            return {
                value: '',
                formData: {
                    day: null,
                    time: '',
                    name: '',
                    num: 1,
                    belongschoolid: 99999,
                }
            }
        }
        ,
        mounted() {

        }
        ,
        methods: {
            ...mapActions(['getGiftList', 'getPeriod', 'setEmpschool', 'deleteEmpschool', 'fillSchoolEmps', 'getAllSchool']),
            daychange(id){
               var obj=this.daysList.find((item)=>{
                    return item.id==id;
                })
                this.formData.name=obj.name;
            },
            timechange(){
                var obj=null;
                if(this.formData.day!=null){
                     obj=this.daysList.find((item)=>{
                        return item.id==this.formData.day;
                    });

                    if(obj!=null) {
                        this.formData.name = obj.name + ' ' + this.formData.time;
                    }
                }

            },

            async submitForm(formName) {
                try {
                    var data = {"data": this.formData}
                    alert(JSON.stringify(this.formData))
                    let result = await addPeriod(data);
                    if (result.success == true) {
                        this.$message({
                            type: 'success',
                            message: '添加成功'
                        });
                        this.getPeriod();
                        this.formData = {
                            day: null,
                            time: '',
                            name: '',
                            num: 1,
                            belongschoolid: 99999,
                        }
                    }
                } catch (err) {
                    console.log(err);
                }
            }
        }
        ,
        computed: {
            ...
                mapGetters(['daysList', 'schoolList', 'masterschoolList'])
        }
        ,
    }
</script>
<style lang="less">
    .el-form-item {
        margin-bottom: 5px;
    }

    .el-form-item__label {
        width: 120px;
        text-align: right;
    }
</style>
